package filter;



import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter(urlPatterns = "/admin/*")
public class AdminFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        HttpSession session = req.getSession();
        Object adminName = session.getAttribute("admin");
        System.out.println(req.getRequestURI());
        if (req.getRequestURI().endsWith("/admin/AdminLogin")) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            if (adminName == null) {
                resp.sendRedirect(req.getContextPath() + "/admin/AdminLogin");
            } else {
                filterChain.doFilter(servletRequest, servletResponse);
            }
        }

    }
}
